package com.sharainy.contoller;

import com.alibaba.fastjson.JSONObject;
import com.sharainy.pojo.Account;
import com.sharainy.pojo.Merchant;
import com.sharainy.pojo.User;
import com.sharainy.service.AccountService;
import com.sharainy.service.MerchantService;
import com.sharainy.util.RestResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/enable")
public class EnableController {
    @Autowired
    private MerchantService merchantService;
    @Autowired
    private AccountService accountService;

    @RequestMapping("/getMerchantMsg")
    @ResponseBody
    public RestResponse getMerchantMsg(HttpServletRequest request){
        HttpSession session = request.getSession();
        User user = (User) session.getAttribute("user");
        JSONObject jsonMain = new JSONObject();
        List<Merchant> merchantList = merchantService.selectAllMerchant(user.getCompanyid()+"");
        jsonMain.put("merchantList",merchantList);
        RestResponse restResponse = RestResponse.succuess();
        restResponse.setData(jsonMain);
        return restResponse;

    }
    @RequestMapping("/getOppenMerchantMsg")
    @ResponseBody
    public RestResponse getOppenMerchantMsg(HttpServletRequest request){
        HttpSession session = request.getSession();
        User user = (User) session.getAttribute("user");
        JSONObject jsonMain = new JSONObject();
        List<Merchant> merchantList = merchantService.selectOppenMerchant(user.getCompanyid()+"");
        jsonMain.put("merchantList",merchantList);
        RestResponse restResponse = RestResponse.succuess();
        restResponse.setData(jsonMain);
        return restResponse;

    }
    @RequestMapping("/merchantDetail")
    @ResponseBody
    public RestResponse getMerchantDetail(String merchantId){
        JSONObject jsonMain = new JSONObject();
        Merchant merchant = merchantService.selectMerchantById(merchantId);
        List<Account> accountList = accountService.selectStartAccount(merchantId);
        jsonMain.put("accountList",accountList);
        jsonMain.put("merchant",merchant);
        RestResponse restResponse = RestResponse.succuess();
        restResponse.setData(jsonMain);
        return restResponse;
    }
    @RequestMapping("modifyMerchant")
    @ResponseBody
    public RestResponse modifyAccount(Merchant merchant){
        try {
            merchantService.updateMerchant(merchant);
            RestResponse restResponse = RestResponse.succuess();
            return restResponse;
        }catch (Exception e){
            RestResponse restResponse = RestResponse.fail();
            return restResponse;
        }

    }
    @RequestMapping("updateMerchantState")
    @ResponseBody
    public RestResponse updateMerchantState(String merchants){
        String[] merIds = merchants.split(",");
        merchantService.updateStates(merIds);
        RestResponse restResponse = RestResponse.succuess();
        return restResponse;
    }
}
